import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/community_business/community_tag_page/subviews/tag_detail_sub_controller.dart';
import 'package:tiktokadult/hj_page/community_business/widget/community_post_widget.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:tiktokadult/routers/jump_router.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

class TagDetailSubView<T extends TagDetailSubController> extends StatelessWidget {
  final TagsBean tag;

  final int? postSort;

  const TagDetailSubView({super.key, required this.tag, this.postSort});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<T>(
      init: instanceController<T>(tag, postSort: postSort),
      global: false,
      builder: (controller) {
        if (controller.isLoading) return LoadingCenterWidget();
        return pullYsRefresh(
          onLoading: (refreshController) => controller.fetchPageData(isRefresh: false),
          onRefresh: (refreshController) => controller.fetchPageData(),
          child: () {
            if (controller.dataSource.isEmpty) return CErrorWidget();
            if (controller.isGridStyle)
              return GridView.builder(
                padding: EdgeInsets.symmetric(horizontal: 16),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2, childAspectRatio: controller.childRatio, crossAxisSpacing: 7, mainAxisSpacing: 12),
                itemCount: controller.dataSource.length,
                itemBuilder: (BuildContext context, int index) {
                  final model = controller.dataSource[index];
                  return GestureDetector(
                    onTap: () => pushToVideoPage(videoModel: model),
                    child: VideoSimpleCellWidget(videoModel: model),
                  );
                },
              );
            return ListView.separated(
              padding: EdgeInsets.only(top: 0),
              separatorBuilder: (_, __) => 14.sizeBoxH,
              itemCount: controller.dataSource.length,
              itemBuilder: (BuildContext context, int index) {
                final model = controller.dataSource[index];
                return CommunityPostWidget(videoModel: model, videoModels: controller.dataSource, showTags: false);
              },
            );
          }(),
          onInit: (ctr) => controller.controller = ctr,
        );
      },
    );
  }
}
